package com.ruoyi.web.controller.mate;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.MatePurRequisition;
import com.ruoyi.domain.ThisStock;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IMatePurRequisitionService;
import com.ruoyi.service.IThisStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用料采购申请Controller
 *
 * @author ruoyi
 * @date 2020-07-06
 */
@Controller
@RequestMapping("/ruoyi/requisition")
public class MatePurRequisitionController extends BaseController {
    private String prefix = "ruoyi/requisition";

    @Autowired
    private IMatePurRequisitionService matePurRequisitionService;
    @Resource
    private IThisStockService thisStockService;

    @RequiresPermissions("ruoyi:requisition:view")
    @GetMapping()
    public String requisition() {
        return prefix + "/requisition";
    }

    /**
     * 查询用料采购申请列表
     */
    @RequiresPermissions("ruoyi:requisition:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Long sid) {
        startPage();
        List<MatePurRequisition> list = matePurRequisitionService.selectMatePurRequisitionLists(sid);
        return getDataTable(list);
    }

    /**
     * 导出用料采购申请列表
     */
    @RequiresPermissions("ruoyi:requisition:export")
    @Log(title = "用料采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MatePurRequisition matePurRequisition) {
        List<MatePurRequisition> list = matePurRequisitionService.selectMatePurRequisitionList(matePurRequisition);
        ExcelUtil<MatePurRequisition> util = new ExcelUtil<MatePurRequisition>(MatePurRequisition.class);
        return util.exportExcel(list, "requisition");
    }

    /**
     * 新增用料采购申请
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存用料采购申请
     */
    @RequiresPermissions("ruoyi:requisition:add")
    @Log(title = "用料采购申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MatePurRequisition matePurRequisition) {
        //自动获取申请人姓名
        matePurRequisition.setmApplicant(ShiroUtils.getLoginName());
        return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//        ThisStock thisStock = new ThisStock();
//        //获取用户输入的id
//        long a = matePurRequisition.getmId();
//        //获取用户选择的材料
//        String name = matePurRequisition.getmName();
//        //查询本厂库存有没有用户用的材料
//        ThisStock thisStock1 = thisStockService.selectThisStockByIds(a, name);
//        //当审批通过时
//        if (matePurRequisition.getmStatus() ==0){
//            return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//        }else if (matePurRequisition.getmStatus() == 2) {
//            //如果返回的对象是空的就代表本厂库存没有所以就向本厂库存添加相应的材料和数量
//            if (null == thisStock1) {
//                thisStock.settId(matePurRequisition.getmId());
//                thisStock.settNum(matePurRequisition.getmNum());
//                thisStock.settName(matePurRequisition.getmName());
//                thisStockService.insertThisStock(thisStock);
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            //如果用户选择的材料本厂库存里有,就只更新材料的数量
//            } else if (matePurRequisition.getmName().equals(thisStock1.gettName())) {
//                long num = (thisStock1.gettNum() + matePurRequisition.getmNum());
//                thisStockService.updateThisStocks(num, name);
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            } else {
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            }
//        }
//        return null;
    }

    /**
     * 修改用料采购申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MatePurRequisition matePurRequisition = matePurRequisitionService.selectMatePurRequisitionById(id);
        mmap.put("matePurRequisition", matePurRequisition);
        return prefix + "/edit";
    }

    /**
     * 修改保存用料采购申请
     */
    @RequiresPermissions("ruoyi:requisition:edit")
    @Log(title = "用料采购申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MatePurRequisition matePurRequisition) {
        return toAjax(matePurRequisitionService.updateMatePurRequisition(matePurRequisition));
    }

    /**
     * 删除用料采购申请
     */
    @RequiresPermissions("ruoyi:requisition:remove")
    @Log(title = "用料采购申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(matePurRequisitionService.deleteMatePurRequisitionByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        matePurRequisitionService.submitExaStatus(id);

        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        int leave = matePurRequisitionService.revoExaStatus(id);
        return success();
    }
}
